// pages/techgoodlist/techgoodlist.js
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,
    inputVal: "",

    search: false, //判断模糊查询方法
    total: '0',
    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },
    startNum: 1, //页数

    curisOnSale: '',

    data: {
      isOnSale: 1, //必选  是否上架
      orderById: 1, //必选  排序编号
      goodsName: '', // 可选  商品名
    },

    listnum: 10, //分页列表个数

  },
  // 搜索
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      search: false
    });
    this.onLoad();
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },

  searInput: function (e) {
    var that = this
    console.log(that.data.inputVal)
    wx.showLoading({
      title: '搜索中',
    })

    that.setData({
      startNum: 1
    })

    if (that.data.inputVal.length != '') {

      var num = that.data.listnum
      var pageNum = that.data.startNum
      var url = 'current=' + pageNum + '&size=' + num
      var data = that.data.data
      data.goodsName = that.data.inputVal

      util.requestNoToken(api.AllGoodList + url, data, "POST").then(function (res) {
        wx.hideLoading();
        if (res.success) {
          var listSize = res.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = that.data.myAgentList.list;
          var lis = res.obj.records;
          lis.forEach(function (item, index, array) {
            array[index] = {
              id: item.id,
              primaryPicUrl: item.primaryPicUrl,
              goodsName: item.goodsName,
              marketPrice: item.marketPrice,
              retailPrice: item.retailPrice,
              categoryName: item.categoryName,
              sellVolume: item.sellVolume,
              goodsBrief: item.goodsBrief
            }
          });
          newData = lis;
          var total1 = res.obj.total;
          var newList = {};
          //当长度小于10表示没有下页数据了
          if (listSize < num) {
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          newList.listSize = listSize;
          that.setData({
            myAgentList: newList
          });
          that.setData({
            startNum: 2,
            total: total1,
            search: true
          });

          if (newList.list.length == 0) {
            wx.showModal({
              title: '提示',
              content: "抱歉！你搜索的商品不存在！",
              confirmColor: "#43CBC9",
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  that.hideInput()
                }
              }
            });
          }

        } else {
          console.log("网络请求失败！")
        }
      }).catch(function (res) {
        wx.hideLoading();
        console.log("获取数据失败！")
      })

    } else {
      wx.showToast({
        title: "输入内容不能为空",
        icon: 'none',
        duration: 2000
      });
    }

  },

  maginfo: function (e) {
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + e.currentTarget.id,
    })
  },

  //上拉获取更多列表
  hotActivityLoadMore: function (e) {
    var that = this
    var search = that.data.search
    if (search) {
      that.listPage(that.data.inputVal)
    } else if (!search) {
      that.listPage('')
    }

  },

  //分页查询列表
  listPage: function (goodsName) {
    var that = this;
    //判断是否还有下一页
    if (that.data.myAgentList.hasMore == true) {
      wx.showLoading({
        title: '加载中',
      })

      var num = that.data.listnum
      var pageNum = that.data.startNum
      var url = 'current=' + pageNum + '&size=' + num
      var data = that.data.data
      data.goodsName = goodsName

      util.requestNoToken(api.AllGoodList + url, data, "POST").then(function (res) {
        wx.hideLoading();
        if (res.success) {
          var listSize = res.obj.records.length;
          var newData = that.data.myAgentList.list;
          var lis = res.obj.records;
          lis.forEach(function (item, index, array) {
            array[index] = {
              id: item.id,
              primaryPicUrl: item.primaryPicUrl,
              goodsName: item.goodsName,
              marketPrice: item.marketPrice,
              retailPrice: item.retailPrice,
              categoryName: item.categoryName,
              sellVolume: item.sellVolume,
              goodsBrief: item.goodsBrief
            }
          });
          //concat是在原有基础上进行增加
          newData = newData.concat(lis);
          var newList = {};
          //当长度等于于0表示当前没有数据，停止
          if (listSize == 0) {
            newList.hasMore = false;
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            return;
            //当长度小于10表示没有下页数据了
          } else if (listSize < num) {
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          newList.listSize = listSize;
          that.setData({
            myAgentList: newList
          });

          that.setData({
            startNum: pageNum + 1 //页数加1
          })
        } else {
          console.log("获取全部商品列表失败")
        }
      }).catch(function (res) {
        wx.hideLoading();
        console.log("获取数据失败！")
      })
    } else {
      wx.showToast({
        title: "已加载全部商品",
        icon: 'none',
        duration: 2000
      });
    }
  },

  //获取商品列表初始值
  getStartList: function (data) {
    var that = this
    var num = that.data.listnum
    var pageNum = that.data.startNum
    var url = 'current=' + pageNum + '&size=' + num;
    wx.showLoading({
      title: '加载中',
    })
    util.requestNoToken(api.AllGoodList + url, data, "POST").then(function (res) {
      if (res.success) {
        wx.hideLoading();
        var listSize = res.obj.records.length;
        var newData = that.data.myAgentList.list;
        var lis = res.obj.records;
        lis.forEach(function (item, index, array) {
          array[index] = {
            id: item.id,
            primaryPicUrl: item.primaryPicUrl,
            goodsName: item.goodsName,
            marketPrice: item.marketPrice,
            retailPrice: item.retailPrice,
            categoryName: item.categoryName,
            sellVolume: item.sellVolume,
            goodsBrief: item.goodsBrief
          }
        });
        newData = lis;
        var total1 = res.obj.total;
        var newList = {};
        //当长度小于5表示没有下页数据了
        if (listSize < num) {
          newList.hasMore = false;
        } else {
          newList.hasMore = true;
        }
        newList.list = newData;
        newList.listSize = listSize;
        that.setData({
          myAgentList: newList,
          total: total1
        });
        that.setData({
          startNum: 2
        })
      } else {
        wx.hideLoading();
        console.log("获取全部商品列表失败")
      }
    }).catch(function (res) {
      wx.hideLoading();
      console.log("获取数据失败！")
    })
  },

  newDataSet:function(){
    var that = this
    var data={
        isOnSale: 1, //必选  是否上架
        orderById: 1, //必选  排序编号
        goodsName: '', // 可选  商品名
    }
    var myAgentList = {
      hasMore: true,
      list: [],
      listSize: 0,
    }

    //数据初始化
    that.setData({
      data: data,
      myAgentList: myAgentList,
      startNum: 1
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this
    that.newDataSet();
    var data = that.data.data
    that.getStartList(data)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})